package com.gitee.cirnochat.user.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.cirnochat.common.exception.BusinessException;
import com.gitee.cirnochat.common.model.enums.ErrorCode;
import com.gitee.cirnochat.common.model.vo.resp.PageVo;
import com.gitee.cirnochat.user.mapper.UserBlockListMapper;
import com.gitee.cirnochat.user.model.entity.UserBlock;
import com.gitee.cirnochat.user.model.vo.resp.UserVo;
import com.gitee.cirnochat.user.service.UserBlockListService;
import com.gitee.cirnochat.user.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author FlanChan
* @description 针对表【user_block_list(用户拉黑列表)】的数据库操作Service实现
* @createDate 2025-04-20 12:35:47
*/
@Service
@RequiredArgsConstructor
public class UserBlockListServiceImpl extends ServiceImpl<UserBlockListMapper, UserBlock>
    implements UserBlockListService {

    private final UserService userService;

    @Override
    public boolean isBlocked(Long userAId, Long userBId) {
        return this.exists(new LambdaQueryWrapper<UserBlock>()
                .eq(UserBlock::getUserId, userAId)
                .eq(UserBlock::getBlockedId, userBId));
    }

    @Override
    public PageVo<UserVo> pageBlockUsers(int page, int size, int sort, Long userId) {
        int offset = (page - 1) * size;
        List<UserVo> records = this.baseMapper.pageBlockUsers(offset, size, userId ,sort).stream()
                .map(e -> new UserVo(e,true))
                .toList();
        long totalCount = this.count(new LambdaQueryWrapper<>(UserBlock.class)
                .like(UserBlock::getUserId, userId));
        int pages = (int) Math.ceil((double) totalCount / size);
        return new PageVo<>(page, size,pages, (int) totalCount,records);
    }

    @Override
    public void addBlockUser(Long blockId, Long userId) {
        if (userId.equals(blockId)) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR,"不能拉黑自己");
        }
        if (!this.userService.existsById(blockId)) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR,"用户不存在");
        }
        if (this.isBlocked(userId, blockId)) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR,"已经拉黑该用户");
        }
        UserBlock userBlock = new UserBlock();
        userBlock.setUserId(userId);
        userBlock.setBlockedId(blockId);
        this.save(userBlock);
    }

    @Override
    public void removeBlockUser(Long blockId, Long userId) {
        if (userId.equals(blockId)) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR,"不能取消拉黑自己");
        }
        if (!this.userService.existsById(blockId)) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR,"用户不存在");
        }
        if (!this.isBlocked(userId, blockId)) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR,"你没有拉黑此用户");
        }
        this.remove(new LambdaQueryWrapper<UserBlock>()
                .eq(UserBlock::getUserId, userId)
                .eq(UserBlock::getBlockedId, blockId));
    }
}




